<template>
  <view class="coupon-details">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-background"></view>
      <view class="nav-content">
      </view>
    </view>

    <!-- 表单内容 -->
    <view class="form-container">
      <scroll-view class="form-scroll" scroll-y enhanced :show-scrollbar="false">
        <view class="form-card">
        <!-- 优惠券名称 -->
        <view class="form-item">
          <text class="form-label">优惠券名称</text>
          <input 
            class="form-input" 
            v-model="formData.couponName" 
            placeholder="请输入优惠券名称"
            placeholder-style="color: #999"
          />
        </view>

        <!-- 优惠券类型 -->
        <view class="form-item">
          <text class="form-label">优惠券类型</text>
          <view class="radio-group">
            <view 
              class="radio-item" 
              :class="{ 'radio-active': formData.couponType === 'reduction' }"
              @click="selectCouponType('reduction')"
            >
              <view class="radio-circle">
                <view v-if="formData.couponType === 'reduction'" class="radio-dot"></view>
              </view>
              <text class="radio-text">满减券</text>
            </view>
            <view 
              class="radio-item" 
              :class="{ 'radio-active': formData.couponType === 'discount' }"
              @click="selectCouponType('discount')"
            >
              <view class="radio-circle">
                <view v-if="formData.couponType === 'discount'" class="radio-dot"></view>
              </view>
              <text class="radio-text">打折券</text>
            </view>
          </view>
          
          <!-- 满减券输入 -->
          <view v-if="formData.couponType === 'reduction'" class="coupon-input">
            <text class="input-text">满</text>
            <input 
              class="amount-input" 
              v-model="formData.fullAmount" 
              placeholder="0"
              type="number"
            />
            <text class="input-text">元减</text>
            <input 
              class="amount-input" 
              v-model="formData.reduceAmount" 
              placeholder="0"
              type="number"
            />
            <text class="input-text">元</text>
          </view>
          
          <!-- 打折券输入 -->
          <view v-if="formData.couponType === 'discount'" class="coupon-input">
            <text class="input-text">打</text>
            <input 
              class="amount-input" 
              v-model="formData.discountRate" 
              placeholder="0"
              type="number"
            />
            <text class="input-text">折</text>
          </view>
        </view>

        <!-- 发放开始时间 -->
        <view class="form-item">
          <text class="form-label">发放开始时间</text>
          <view class="date-picker" @click="selectStartDate">
            <text class="date-text">{{ formData.startDate || '请选择开始时间' }}</text>
            <uni-icons type="right" size="16" color="#999" />
          </view>
        </view>

        <!-- 发放结束时间 -->
        <view class="form-item">
          <text class="form-label">发放结束时间</text>
          <view class="date-picker" @click="selectEndDate">
            <text class="date-text">{{ formData.endDate || '请选择结束时间' }}</text>
            <uni-icons type="right" size="16" color="#999" />
          </view>
        </view>

        <!-- 投放数量 -->
        <view class="form-item">
          <text class="form-label">投放数量</text>
          <view class="input-with-unit">
            <input 
              class="form-input" 
              v-model="formData.quantity" 
              placeholder="请输入投放数量"
              type="number"
              placeholder-style="color: #999"
            />
            <text class="unit-text">张</text>
          </view>
        </view>

        <!-- 每人限领 -->
        <view class="form-item">
          <text class="form-label">每人限领</text>
          <view class="input-with-unit">
            <input 
              class="form-input" 
              v-model="formData.limitPerPerson" 
              placeholder="请输入每人限领数量"
              type="number"
              placeholder-style="color: #999"
            />
            <text class="unit-text">张</text>
          </view>
        </view>

        <!-- 使用有效期 -->
        <view class="form-item">
          <text class="form-label">使用有效期</text>
          <view class="date-range">
            <view class="date-picker" @click="selectValidStartDate">
              <text class="date-text">{{ formData.validStartDate || '开始时间' }}</text>
              <uni-icons type="right" size="16" color="#999" />
            </view>
            <text class="date-separator">至</text>
            <view class="date-picker" @click="selectValidEndDate">
              <text class="date-text">{{ formData.validEndDate || '结束时间' }}</text>
              <uni-icons type="right" size="16" color="#999" />
            </view>
          </view>
        </view>
        
        <!-- 确认投放按钮 -->
        <view class="action-section">
          <button class="confirm-btn" @click="confirmIssue">
            <text class="btn-text">确认投放</text>
          </button>
        </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        couponName: '',
        couponType: 'reduction', // 'reduction' 或 'discount'
        fullAmount: '',
        reduceAmount: '',
        discountRate: '',
        startDate: '2023-03-18',
        endDate: '2023-03-18',
        quantity: '',
        limitPerPerson: '',
        validStartDate: '',
        validEndDate: ''
      }
    };
  },
  methods: {
    selectCouponType(type) {
      this.formData.couponType = type;
      // 清空相关输入
      if (type === 'reduction') {
        this.formData.discountRate = '';
      } else {
        this.formData.fullAmount = '';
        this.formData.reduceAmount = '';
      }
    },
    selectStartDate() {
      uni.showModal({
        title: '选择发放开始时间',
        content: '这里可以集成日期选择器',
        showCancel: false
      });
    },
    selectEndDate() {
      uni.showModal({
        title: '选择发放结束时间',
        content: '这里可以集成日期选择器',
        showCancel: false
      });
    },
    selectValidStartDate() {
      uni.showModal({
        title: '选择使用开始时间',
        content: '这里可以集成日期选择器',
        showCancel: false
      });
    },
    selectValidEndDate() {
      uni.showModal({
        title: '选择使用结束时间',
        content: '这里可以集成日期选择器',
        showCancel: false
      });
    },
    confirmIssue() {
      // 表单验证
      if (!this.formData.couponName) {
        uni.showToast({
          title: '请输入优惠券名称',
          icon: 'none'
        });
        return;
      }
      
      if (this.formData.couponType === 'reduction') {
        if (!this.formData.fullAmount || !this.formData.reduceAmount) {
          uni.showToast({
            title: '请输入满减金额',
            icon: 'none'
          });
          return;
        }
      } else {
        if (!this.formData.discountRate) {
          uni.showToast({
            title: '请输入折扣率',
            icon: 'none'
          });
          return;
        }
      }
      
      if (!this.formData.quantity || !this.formData.limitPerPerson) {
        uni.showToast({
          title: '请输入投放数量和限领数量',
          icon: 'none'
        });
        return;
      }
      
      // 确认投放
      uni.showModal({
        title: '确认投放',
        content: '确定要投放这张优惠券吗？',
        success: (res) => {
          if (res.confirm) {
            uni.showToast({
              title: '投放成功',
              icon: 'success'
            });
            // 延迟返回上一页
            setTimeout(() => {
              uni.navigateBack();
            }, 1500);
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.coupon-details {
  min-height: 100vh;
  background: #f5f5f5;
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  margin: 0;
  padding: 0;
}

/* 导航栏样式 */
.nav-bar {
  position: relative;
  z-index: 10;
  height: 140px;
  padding-top: 30px;
  overflow: hidden;
}

.nav-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #1F73F1;
  box-shadow: 0 8px 32px rgba(31, 115, 241, 0.3);
}

.nav-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  height: 110px;
  padding: 0 20px 20px;
}

.nav-left {
  padding: 10px;
  margin-left: -10px;
  border-radius: 50%;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.nav-left:active {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(0.95);
}

.nav-title {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.title-text {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav-icon-wrapper {
  padding: 8px;
  border-radius: 50%;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.nav-icon-wrapper:active {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(0.95);
}

/* 表单容器 - 绝对定位脱离文档流，包含圆角和背景 */
.form-container {
  position: absolute;
  top: 10px;
  left: 20px;
  right: 20px;
  z-index: 10;
  height: calc(100vh - 20px);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  box-sizing: border-box;
}

/* 滚动容器 */
.form-scroll {
  height: 100%;
  width: 100%;
}

.form-card {
  background: transparent;
  padding: 24px 20px 0;
  width: 100%;
  min-height: calc(100vh - 40px);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

/* 表单项 */
.form-item {
  margin-bottom: 24px;
}

.form-item:last-child {
  margin-bottom: 0;
}

.form-label {
  display: block;
  font-size: 16px;
  color: #333;
  font-weight: 500;
  margin-bottom: 12px;
}

.form-input {
  width: 100%;
  height: 48px;
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 0 16px;
  font-size: 16px;
  color: #333;
  box-sizing: border-box;
}

.form-input:focus {
  border-color: #3388FF;
  background: #fff;
}

/* 单选框组 */
.radio-group {
  display: flex;
  gap: 24px;
  margin-bottom: 16px;
}

.radio-item {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.radio-item:active {
  transform: scale(0.98);
}

.radio-circle {
  width: 20px;
  height: 20px;
  border: 2px solid #ddd;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.radio-active .radio-circle {
  border-color: #3388FF;
}

.radio-dot {
  width: 10px;
  height: 10px;
  background: #3388FF;
  border-radius: 50%;
}

.radio-text {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

/* 优惠券输入 */
.coupon-input {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
}

.input-text {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.amount-input {
  flex: 1;
  height: 32px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 8px;
  font-size: 16px;
  color: #333;
  text-align: center;
}

.amount-input:focus {
  border-color: #3388FF;
}

/* 日期选择器 */
.date-picker {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 0 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.date-picker:active {
  background: #e9ecef;
}

.date-text {
  font-size: 16px;
  color: #333;
}

/* 带单位的输入 */
.input-with-unit {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input-with-unit .form-input {
  flex: 1;
}

.unit-text {
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

/* 日期范围 */
.date-range {
  display: flex;
  align-items: center;
  gap: 12px;
}

.date-range .date-picker {
  flex: 1;
}

.date-separator {
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

/* 操作按钮区域 - 固定在表单容器底部 */
.action-section {
  position: sticky;
  bottom: 0;
  padding: 20px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-top: 1px solid #f0f0f0;
  width: 100%;
  box-sizing: border-box;
  margin-top: auto;
}

.confirm-btn {
  width: 100%;
  height: 56px;
  background: linear-gradient(135deg, #3388FF 0%, #3388FF 100%);
  border: none;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 32px rgba(51, 136, 255, 0.3);
  transition: all 0.3s ease;
}

.confirm-btn:active {
  transform: scale(0.98);
  box-shadow: 0 4px 16px rgba(51, 136, 255, 0.4);
}

.btn-text {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
}
</style>
